import router from "./router";
import store from "./store";
import { Message } from "element-ui";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style
import { getToken } from "@/utils/auth"; // get token from cookie
import getPageTitle from "@/utils/get-page-title";
import PromptTip from "@/components/LoginDialog";
NProgress.configure({ showSpinner: false }); // NProgress Configuration

const metaList = [
	"/",
	"/news",
	"/hk",
	"/usa",
	"/columns",
	"/columnlist",
	"/events",
];

const blockList = [
	"/my/Individual",
	"/my/mymessage",
	"/my/realname",
	"/my/follow",
	"/my/collect",
	"/my/events",
];
router.beforeEach(async (to, from, next) => {
	// start progress bar
	NProgress.start();

	var path = to.path;

	var title = "";
	var desc = "";

	if (path == "/") {
		title = "Web3媒体| 加密货币资讯| 区块链新闻| MetaEra";
		desc =
			"获取最新的Web3行情资讯和实时新闻，尽在MetaEra，您的区块链媒体首选平台。关注我们，掌握行业动态！";
	} else if (path == "/news") {
		title = "Web3 快讯 | 区块链快讯 |加密货币快讯| MetaEra";
		desc =
			"及时获取Web3行情快讯，关注区块链最新动态，尽在MetaEra。让您不错过每一个重要消息！";
	} else if (path == "/hk") {
		title = "香港Web3资讯  | 香港Web3政策 | MetaEra";
		desc =
			"获取最新香港Web3资讯和政策新闻，尽在MetaEra。关注香港区块链动态，助您把握行业发展脉搏！";
	} else if (path == "/usa") {
		title = "美国Web3资讯| 美国加密货币资讯 |美国加密货币政策| MetaEra";
		desc =
			"获取最新美国Web3资讯和政策新闻，尽在MetaEra。关注美国区块链动态，助您掌握行业发展趋势！";
	} else if (path == "/columns") {
		title = "MetaEra专栏 | Web3 行业专栏 | 区块链专栏";
		desc =
			"探索Web3资讯和深度内容，尽在MetaEra区块链专栏。获取行业洞察，助您把握未来趋势与机会！";
	} else if (path == "/columnlist") {
		title = "MetaEra专栏作者 | 创作达人 | MetaEra";
		desc =
			"关注Web3资讯和深度内容创作者，尽在MetaEra区块链专栏。获取最新观点，助您在行业中脱颖而出！";
	} else if (path == "/events") {
		title = "Web3活动 | 区块链活动 | MetaEra活动";
		desc =
			"获取最新Web3展会和活动票务信息，尽在MetaEra区块链会务。掌握行业盛会，轻松参与精彩活动！";
	}
	var fullPath = "https://www.metaera.hk" + to.fullPath;
	let ogurl = document.querySelector('meta[property="og:url"]');
	if (!ogurl) {
		ogurl = document.createElement("meta");
		ogurl.setAttribute("property", "og:url");
		document.head.appendChild(ogurl);
	}
	ogurl.setAttribute("content", fullPath);

	let twitterurl = document.querySelector('meta[name="twitter:url"]');
	if (!twitterurl) {
		twitterurl = document.createElement("meta");
		twitterurl.setAttribute("name", "twitter:url");
		document.head.appendChild(twitterurl);
	}
	twitterurl.setAttribute("content", fullPath);

	let ogtype = document.querySelector('meta[property="og:type"]');
	if (!ogtype) {
		ogtype = document.createElement("meta");
		ogtype.setAttribute("property", "og:type");
		document.head.appendChild(ogtype);
	}
	ogtype.setAttribute("content", "article");

	let ogimage = document.querySelector('meta[property="og:image"]');
	if (!ogimage) {
		ogimage = document.createElement("meta");
		ogimage.setAttribute("property", "og:image");
		document.head.appendChild(ogimage);
	}
	ogimage.setAttribute(
		"content",
		"https://res.metaera.hk/resources/MetaEra_logo.png"
	);

	let twittercard = document.querySelector('meta[name="twitter:card"]');
	if (!twittercard) {
		twittercard = document.createElement("meta");
		twittercard.setAttribute("name", "twitter:card");
		document.head.appendChild(twittercard);
	}
	twittercard.setAttribute("content", "summary");

	let twitterimage = document.querySelector('meta[name="twitter:image"]');
	if (!twitterimage) {
		twitterimage = document.createElement("meta");
		twitterimage.setAttribute("name", "twitter:image");
		document.head.appendChild(twitterimage);
	}
	twitterimage.setAttribute(
		"content",
		"https://res.metaera.hk/resources/MetaEra_logo.png"
	);

	// 触发 Twitter Cards 更新
	if (typeof twttr !== "undefined") {
		twttr.widgets.load();
	}

	if (title == null || title == "") {
	} else {
		document.title = title;

		let ogtitle = document.querySelector('meta[property="og:title"]');
		if (!ogtitle) {
			ogtitle = document.createElement("meta");
			ogtitle.setAttribute("property", "og:title");
			document.head.appendChild(ogtitle);
		}
		ogtitle.setAttribute("content", title);

		let ogdescription = document.querySelector(
			'meta[property="og:description"]'
		);
		if (!ogdescription) {
			ogdescription = document.createElement("meta");
			ogdescription.setAttribute("property", "og:description");
			document.head.appendChild(ogdescription);
		}
		ogdescription.setAttribute("content", desc);

		let ogsite_name = document.querySelector('meta[property="og:site_name"]');
		if (!ogsite_name) {
			ogsite_name = document.createElement("meta");
			ogsite_name.setAttribute("property", "og:site_name");
			document.head.appendChild(ogsite_name);
		}
		ogsite_name.setAttribute("content", title);

		let metaTitle = document.querySelector("title");
		if (!metaTitle) {
			metaTitle = document.createElement("title");
			document.head.appendChild(metaTitle);
		}
		metaTitle.setAttribute("content", title);

		let description = document.querySelector('meta[name="description"]');
		if (!description) {
			description = document.createElement("meta");
			description.setAttribute("name", "description");
			document.head.appendChild(description);
		}
		description.setAttribute("content", desc);

		let twittertitle = document.querySelector('meta[name="twitter:title"]');
		if (!twittertitle) {
			twittertitle = document.createElement("meta");
			twittertitle.setAttribute("name", "twitter:title");
			document.head.appendChild(twittertitle);
		}
		twittertitle.setAttribute("content", title);

		let twitterdescription = document.querySelector(
			'meta[name="twitter:description"]'
		);
		if (!twitterdescription) {
			twitterdescription = document.createElement("meta");
			twitterdescription.setAttribute("name", "twitter:description");
			document.head.appendChild(twitterdescription);
		}
		twitterdescription.setAttribute("content", desc);
	}

	// determine whether the user has logged in
	const hasToken = getToken();

	if (hasToken) {
		const hasGetUserInfo = store.getters.name;
		if (hasGetUserInfo) {
			next();
		} else {
			try {
				await store.dispatch("user/getInfo");
				next({ ...to, replace: true });
			} catch (error) {
				await store.dispatch("user/resetToken");
				Message.error(error || "Has Error");
				next(`/home`);
				NProgress.done();
			}
		}
	} else {
		/* has no token*/
		if (blockList.indexOf(to.name) !== -1) {
			next(`/home`);
			PromptTip({ dialogVisible: true, showStatus: 1 });
			NProgress.done();
		} else {
			next();
		}
	}
});

router.afterEach(() => {
	// finish progress bar
	NProgress.done();
});
